<template>
  <table>
    <thead>
      <tr>
        <th></th>
        <th>歌曲标题</th>
        <th>时长</th>
        <th>歌手</th>
        <th>专辑</th>
      </tr>
    </thead>
    <tbody>
      <tr :class="{current: index%2 === 0}" v-for="(item,index) in playlist" :key="item.id">
        <td>{{index + 1}}</td>
        <td @click="toSongDetail(item.id)">
          <div class="name">
            <span class="play"></span>
            {{item.name}}
          </div>
        </td>
        <td>{{item.dt | timer}}</td>
        <td>
          <div class="artist">{{item.ar[0].name}}</div>
        </td>
        <td>
          <div class="album">{{item.al.name}}</div>
        </td>
      </tr>
    </tbody>
  </table>
</template>

<script>
import { formatDate } from '@/common/format'

export default {
  props: {
    playlist: {
      type: Array,
      default() {
        return []
      }
    }
  },
  filters: {
    timer(time) {
      return formatDate(time, 'mm:ss')
    }
  },
  methods: {
    // 跳转到歌曲详情页
    toSongDetail(songId) {
      this.$router.push('/discover/song/detail?id=' + songId)
    }
  }
}
</script>

<style scoped>
table {
  width: 100%;
  box-sizing: border-box;
  border: 1px solid #ccc;
  border-collapse: collapse;
  font-size: 12px;
  color: #000;
}

th,
td {
  padding-left: 15px;
  line-height: 30px;
}
tr td:nth-child(1) {
  padding: 0;
  text-align: center;
}

/* 表格标题行 */
th {
  color: #666;
  text-align: left;
  height: 40px;
  box-sizing: border-box;
  border-right: 1px solid #ccc;
  border-bottom: 1px solid #ccc;
}
tr {
  width: 100%;
}
thead tr th:nth-child(1) {
  width: 10%;
}
thead tr th:nth-child(2) {
  width: 35%;
}
thead tr th:nth-child(3) {
  width: 15%;
}
thead tr th:nth-child(4) {
  width: 20%;
}
thead tr th:nth-child(5) {
  border-right: none;
  width: 20%;
}

/* 播放按钮 */
.play {
  display: inline-block;
  vertical-align: middle;
  margin-right: 5px;
  width: 17px;
  height: 17px;
  background: url(~@/assets/img/table.png) 0 -103px;
  cursor: pointer;
}
.play:hover {
  background-position-y: -128px;
}

/* 歌名、歌手名、专辑名 过长不换行 */
.name {
  width: 200px;
}
.artist {
  width: 100px;
}
.album {
  width: 100px;
}
.name,
.artist,
.album {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  cursor: pointer;
}
.name:hover,
.artist:hover,
.album:hover {
  text-decoration: underline;
}

/* 隔行变色 */
.current {
  background-color: #f7f7f7;
}
</style>